// pages/Authorization/index.js
//index.js
//获取应用实例
const app = getApp();


Page({
  globalData: {
    userInfo: {},
    hasUserInfo: false,
    userAddress: null
  },
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

  },
  //事件处理函数
  getUserInfo: function (e) {
    wx.login({
      success(res){
        console.log(e)
        wx.request({
          url:  getApp().globalData.url+'/onlogin.do',
          method: "GET",
          data: {
            code: res.code,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          },
          header: {
            'content-type': 'json'
          },
          success: function (res) {
            console.log(res);
            getApp().globalData.openid = res.data.openid;
            getApp().globalData.tollgate = res.data.tollgate;
            if(res.data.data == 1){
              wx.redirectTo({
                url: '../index/index',
              })
            }else{
              wx.redirectTo({
                url: '../Authorization/index',
              })
            }
            
          },
          fail: function(res){
            wx.redirectTo({
              url: '../Authorization/index',
            })
          }
        })
      }
    })
    
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })

    
    console.log(app.globalData.userInfo)
  },

  onLoad: function () {
   
    wx.getSetting({
      success(res) {
        console.log("登陆后的用户授权设置：" + res.authSetting);
        if (!res.authSetting['scope.userInfo']) {

        }
      }
    })

  },
  onShow: function () {

  }
})






